﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace GameName1
{
    class AngryAlien : Alien
    {
        private static Random _rdm;

        private static List<Texture2D> _commonSprite = new List<Texture2D>();
        protected override Texture2D Sprite
        {
            get
            {
                if (_id < 11)
                    return AngryAlien._commonSprite[2];

                if (_id >= 11 && _id < 33)
                    return AngryAlien._commonSprite[0];

                if (_id >= 33)
                    return AngryAlien._commonSprite[1];

                return null;
            }
        }
        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Sprite, GetDestinationRectangle(), GetSourceRectangle(), Color.White);
        }
        private Rectangle GetSourceRectangle()
        {
            return new Rectangle((Sprite.Width / 2) * (_frame % 2), 0, Sprite.Width / 2, Sprite.Height);
        }
        private Rectangle GetDestinationRectangle()
        {
            return new Rectangle((int)Position.X, (int)Position.Y, Sprite.Width / 2, Sprite.Height);
        }
        
        public static AngryAlienState AngryAlienState { get; set; }
        public static double FireRate;
        
        public Weapon Weapon { private get; set; }

        private int _frame;
        private int _id;
        
        private double _lastShootTime;

        public AngryAlien(Vector2 position, int id) : base(position)
        {
            _id = id;           

            _rdm = new Random();

            if (_id < 11)
                Points = 40;

            if (_id >= 11 && _id < 33)
                Points = 20;

            if (_id >= 33)
                Points = 10;
        }

        public override void Update(GameTime gameTime)
        {
          
            if (gameTime.TotalGameTime.TotalMilliseconds > _lastShootTime + FireRate)
            {
                TryShoot();
                _lastShootTime = gameTime.TotalGameTime.TotalMilliseconds;
            }
            
        }
        public static void LoadContent(ContentManager contentManager)
        {
            _commonSprite.Add(contentManager.Load<Texture2D>("sprites/invader1_sheet_v2"));
            _commonSprite.Add(contentManager.Load<Texture2D>("sprites/invader2_sheet_v2"));
            _commonSprite.Add(contentManager.Load<Texture2D>("sprites/invader3_sheet_v2"));
        }
        
        private bool NoAngryAliensAhead()
        {
            var angryAliens = GameObjectsManager.GetInstance().GetAllObjectsOfType<AngryAlien>();
            var ray = new Rectangle((int)Position.X, (int)Position.Y, GetDestinationRectangle().Width, (int)(ScreenManager.GetInstance().Resolution.Y - Position.Y));

            return angryAliens.Where(otherAlien => otherAlien != this).All(otherAlien => !otherAlien.GetDestinationRectangle().Intersects(ray));
        }
        private void TryShoot()
        {
            if (NoAngryAliensAhead() && _rdm.NextDouble() > 0.95)
            {
                var spawnPoint = new Vector2(Position.X + GetDestinationRectangle().Width / 2, Position.Y + Sprite.Height + 5);
                Weapon.Fire<Missile>(spawnPoint);
            }
        }

        public override void HandleHit(IHittable other, GameTime colissionTime)
        {
            var explosion = new AngryAlienExplosion(Position, colissionTime);
            GameObjectsManager.GetInstance().Instantiate(explosion);

            base.HandleHit(other, colissionTime);
        }
        public override Rectangle GetCollisionBox()
        {
            return GetDestinationRectangle();
        }

        public override void Move()
        {     
            AngryAlienState.Move(this);
            _frame++;
        }       
    }
}
